package com.java.exer_blackjack;

import java.util.Arrays;

/**
 * @author shkstart
 * @create 2021-01-13 15:18
 */
public class Player {
    private String name;
    private Poker[] pokers;

    public Player(String name) {
        super();
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Poker[] getPokers() {
        return pokers;
    }

    public void setPokers(Poker[] pokers) {
        this.pokers = pokers;
    }

    @Override
    public String toString() {
        String info = "[暗牌, ";
        for (int i = 0; i < pokers.length; i++) {
            info += pokers[i].toString();
            if (i != pokers.length - 1) {
                info += ", ";
            }

        }
        return this.name + ": \n" +
                info + "]";
    }

    public String getInfo() {
        return this.name + ": \n" +
                Arrays.toString(this.pokers);
    }

    /*
     *获取点数
     *
     * */
    public int getPoint() {
        int pointers = 0;
        int countA = 0;
        for (Poker poker : pokers) {
            if (poker.getNum() == 1) {
                pointers += 11;
                countA++;
            } else if (poker.getNum() > 10) {
                pointers += 10;
            } else {
                pointers += poker.getNum();
            }
        }

        if (pointers > 21) {
            pointers -= countA * 10;
        }
        return pointers;
    }

    /**
     * 收牌
     */
    public void getPoker(Poker poker) {
        //判断进来的牌是否为空，如果是空 就给他new一个数组长度
        if (this.pokers == null) {
            this.pokers = new Poker[1];
        } else {
            //如果这个数组已经有了元素，就给一个新的数组new这个原来数组的长度+1
            Poker[] newPoker = new Poker[pokers.length + 1];
            //把原来数组的值用循环赋给新的数组
            for (int i = 0; i < pokers.length; i++) {
                newPoker[i] = pokers[i];

            }
            //把新的数组的地址给原数组
            pokers = newPoker;
        }

        pokers[pokers.length - 1] = poker;
    }

}
